package com.flansmod.common.driveables;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;

public class ContainerDriveableInventory extends Container
{
	// 玩家背包
	public InventoryPlayer inventory;
    public World world;
	//载具对象
	public EntityDriveable plane;
	public int numItems;
	//目前玩家在哪个GUI里
	public int screen;
	public int maxScroll;
	public int scroll;
	public DriveableType type;

	@Override
	public ItemStack slotClick(int slotIndex, int button, int modifier, EntityPlayer player) {
		Slot slot;
		// 获取点击的槽位
		if (slotIndex >= 0 && slotIndex < this.inventorySlots.size()){
			 slot = this.getSlot(slotIndex);
		}
		else {
			 slot = null;
		}
		if (slot != null) {
			// 检查按钮和修饰键的值，这里我们只关注左键点击事件
			if (button == 0 && modifier == 0) {
				// 在这里判断是否禁止放置物品的条件
				if (shouldPreventItemPlacement(slot.getStack())) {
					return null; // 返回一个空物品栈，表示不允许放置物品
				}
			}
		}

		// 调用父类的方法处理其他情况
		return super.slotClick(slotIndex, button, modifier, player);
	}

    public ContainerDriveableInventory(InventoryPlayer inventoryplayer, World world, EntityDriveable entPlane, int i)
    {
		inventory = inventoryplayer;
        this.world = world;
		plane = entPlane;
		type = plane.getDriveableType();

		screen = i;
		//在库存里的物品数量
		numItems = 0;
		switch(i)
		{
			case 0 :
			{
				numItems = plane.driveableData.numGuns;
				maxScroll = (numItems > 3 ? numItems - 3 : 0);
				break;
			}
			case 1 : 
			{
				numItems = plane.getDriveableType().numBombSlots;
				maxScroll = (((numItems + 7) / 8) > 3 ? ((numItems + 7) / 8) - 3 : 0);
				break;
			}
			case 2 : 
			{
				numItems = plane.getDriveableType().numCargoSlots;
				maxScroll = (((numItems + 7) / 8) > 3 ? ((numItems + 7) / 8) - 3 : 0);
				break;
			}	
			case 3 : 
			{
				numItems = plane.getDriveableType().numMissileSlots;
				maxScroll = (((numItems + 7) / 8) > 3 ? ((numItems + 7) / 8) - 3 : 0);
				break;
			}	
		}
		
		//Add screen specific slots
		switch(screen)
		{
			case 0 : //Guns
			{
				int slotsDone = 0;
				for(int j = 0; j < plane.driveableData.numGuns; j++)
				{
					int yPos = -1000;
					if(slotsDone < 3 + scroll && slotsDone >= scroll)
						yPos = 25 + 19 * slotsDone;
					addSlotToContainer(new SlotDriveableAmmunition(plane.driveableData, j, 29, yPos, type.filterAmmunition));
					slotsDone++;
				}	
				break;
			}
			case 1 : //Bombs
			case 2 : //Cargo
			case 3 : //Missiles
			{
				int startSlot = plane.driveableData.getBombInventoryStart();
				if(screen == 2)
					startSlot = plane.driveableData.getCargoInventoryStart();
				if(screen == 3)
					startSlot = plane.driveableData.getMissileInventoryStart();
				int m = ((numItems + 7) / 8);
				for(int row = 0; row < m; row++)
				{
					int yPos = -1000;
					if(row < 3 + scroll && row >= scroll)
						yPos = 25 + 19 * (row - scroll);
					for(int col = 0; col < ((row + scroll + 1) * 8 <= numItems ? 8 : numItems % 8); col++)
					{
						addSlotToContainer(new SlotDriveableAmmunition(plane.driveableData, startSlot + row * 8 + col, 10 + 18 * col, yPos, type.filterAmmunition));
					}
				}
				break;
			}
		}
		
		//Main inventory slots
        for(int row = 0; row < 3; row++)
        {
            for(int col = 0; col < 9; col++)
            {
            	addSlotToContainer(new Slot(inventoryplayer, col + row * 9 + 9, 8 + col * 18, 98 + row * 18));
            }

        }
		//Quickbar slots
        for(int col = 0; col < 9; col++)
        {
        	addSlotToContainer(new Slot(inventoryplayer, col, 8 + col * 18, 156));
        }
    }
	
	public void updateScroll(int scrololol)
	{
		scroll = scrololol;
		switch(screen)
		{
			case 0 :
			{
				int slotsDone = 0;
				for(int i = 0; i < plane.driveableData.numGuns; i++)
				{
					int yPos = -1000;
					if(slotsDone < 3 + scroll && slotsDone >= scroll)
						yPos = 25 + 19 * (slotsDone - scroll);
					((Slot)inventorySlots.get(slotsDone)).yDisplayPosition = yPos;
					slotsDone++;
				}	
				break;
			}
			case 1 :
			case 2 :
			case 3 : 
			{
				int m = ((numItems + 7) / 8);
				for(int row = 0; row < m; row++)
				{
					int yPos = -1000;
					if(row < 3 + scroll && row >= scroll)
						yPos = 25 + 19 * (row - scroll);
					for(int col = 0; col < ((row + 1) * 8 <= numItems ? 8 : numItems % 8); col++)
					{
						((Slot)inventorySlots.get(row * 8 + col)).yDisplayPosition = yPos;
					}
				}
				break;
			}
		}
	}

	@Override
    public boolean canInteractWith(EntityPlayer entityplayer)
    {
		return true;
    }

	private boolean shouldPreventItemPlacement(ItemStack stack) {
		// 在此处添加你的条件判断逻辑
		// 如果满足条件，返回 true 来禁止物品放置操作
		// 否则返回 false
		// 例如，你可以通过物品的ID、标签或其他属性来判断是否禁止放置
		return stack != null && (stack.getItem() instanceof ItemVehicle || stack.getItem() instanceof ItemPlane);
	}
	@Override
	public ItemStack transferStackInSlot(EntityPlayer player, int slotID) {
		ItemStack stack = null;
		Slot currentSlot = (Slot) inventorySlots.get(slotID);
		if (currentSlot != null && currentSlot.getHasStack() && shouldPreventItemPlacement(currentSlot.getStack())) {
			return null; // 取消物品放置操作
		}
		if (currentSlot != null && currentSlot.getHasStack()) {
			ItemStack slotStack = currentSlot.getStack(); // 目前对应物品栏里物品堆
			stack = slotStack.copy(); // 复制了一个?

			if (slotID >= numItems) {
				if (!mergeItemStack(slotStack, 0, numItems, false)) {
					return null;
				}
			}
			else {
				if (!mergeItemStack(slotStack, numItems, inventorySlots.size(), true)) {
					return null;
				}
			}

			if (slotStack.stackSize == 0) {
				currentSlot.putStack(null);
			} else {
				currentSlot.onSlotChanged();
			}

			if (slotStack.stackSize == stack.stackSize) {
				return null;
			}

			currentSlot.onPickupFromSlot(player, slotStack);
		}

		return stack;
	}	// Code modified from https://www.minecraftforge.net/forum/topic/34525-18-solved-attempt-to-fix-mergeitemstack-isnt-working/
	@Override
	protected boolean mergeItemStack(ItemStack stack, int startIndex, int endIndex, boolean reverseDirection) {
		boolean flag = false;
		int i = startIndex;
		if (reverseDirection)
			i = endIndex - 1;
		
		if (stack.isStackable()) {
			while (stack.stackSize > 0 && (!reverseDirection && i < endIndex || reverseDirection && i >= startIndex)) {
				Slot slot = (Slot) this.inventorySlots.get(i);
				ItemStack itemstack = slot.getStack();
				int maxLimit = Math.min(stack.getMaxStackSize(), slot.getSlotStackLimit());
				
				if (itemstack != null && ItemStack.areItemStacksEqual(stack, itemstack)) {
					int j = itemstack.stackSize + stack.stackSize;
					if (j <= maxLimit) {
						stack.stackSize = 0;
						itemstack.stackSize = j;
						slot.onSlotChanged();
						flag = true;
						
					} else if (itemstack.stackSize < maxLimit) {
						stack.stackSize -= maxLimit -itemstack.stackSize;
						itemstack.stackSize = maxLimit;
						slot.onSlotChanged();
						flag = true;
					}
				}
				if (reverseDirection) { 
					--i;
				} else
					++i;
			}
		}
		if (stack.stackSize > 0) {
			if (reverseDirection) {
				i = endIndex - 1;
			}else i = startIndex;

			while (!reverseDirection && i < endIndex || reverseDirection && i >= startIndex) {
				Slot slot1 = (Slot)this.inventorySlots.get(i);
				ItemStack itemstack1 = slot1.getStack();

				if (itemstack1 == null && slot1.isItemValid(stack)) {
					if(stack.stackSize <= slot1.getSlotStackLimit()) {
						slot1.putStack(stack.copy());
						slot1.onSlotChanged();
						stack.stackSize = 0;
						flag = true;
						break;
					} else {
						itemstack1 = stack.copy();
						stack.stackSize -= slot1.getSlotStackLimit();
						itemstack1.stackSize = slot1.getSlotStackLimit();
						slot1.putStack(itemstack1);
						slot1.onSlotChanged();
						flag = true;
					}					
				}
				if (reverseDirection) {
					--i;
				} else ++i;
			}
		}
		return flag;
	}
	
	
}
